package edu.hawaii.its381b.client;

import com.google.gwt.canvas.client.Canvas;
import com.google.gwt.dom.client.ImageElement;
import com.google.gwt.safehtml.shared.SafeUri;
import com.google.gwt.user.client.ui.Image;

public class GameMap {
	private ImageElement gameMapImage, blackBarImage;
	private Canvas canvas;
	private int canvasWidth, canvasHeight, mapWidth, mapHeight,
				mapDrawnWidth, mapDrawnHeight,
				mapCanvasDrawUpperLeftX = 0, mapCanvasDrawUpperLeftY = 0,
				lastMapDrawUpperLeftRegionX = 0, lastMapDrawUpperLeftRegionY = 0,
				crabBoundary, CrabSizepx = 50,
	//controlBarThickness is where the map is drawn so it doesn't cover the black control border area
				controlBarThickness; 
	
	
	public GameMap(int canvasWidthAmount, int canvasHeightAmount,
				   int mapWidthAmount, int mapHeightAmount,
				   int crabStartX, int crabStartY,
				   int crabCanvasStartX, int crabCanvasStartY,
				   int controlBarThicknessAmount, int crabBoundaryAmount,Canvas canvas){
		this.canvas = canvas;
		canvasWidth = canvasWidthAmount;
		canvasHeight = canvasHeightAmount;
		mapWidth = mapWidthAmount;
		mapHeight = mapHeightAmount;
		//the outer half of borderAmount is for the black control area
		//the inner half of borderAmount contains the map drawing region
		controlBarThickness= controlBarThicknessAmount;
		crabBoundary = crabBoundaryAmount;

		mapDrawnWidth = canvasWidthAmount - controlBarThicknessAmount*2;
		mapDrawnHeight = canvasHeightAmount - controlBarThicknessAmount*2;
		
		mapCanvasDrawUpperLeftX = canvasWidth/2-crabStartX;
		mapCanvasDrawUpperLeftY = canvasHeight/2-crabStartY;
		lastMapDrawUpperLeftRegionX=mapCanvasDrawUpperLeftX;
		lastMapDrawUpperLeftRegionY=mapCanvasDrawUpperLeftY;
		
		SafeUri gameMapURI = HermitCrabResources.INSTANCE.GameMap().getSafeUri();
		this.gameMapImage = ImageElement.as((new Image(gameMapURI )).getElement());
		
       	SafeUri blackBarURI = HermitCrabResources.INSTANCE.blackBar().getSafeUri();
    	blackBarImage =  ImageElement.as((new Image(blackBarURI )).getElement());
		
		draw(crabStartX,crabStartY,crabCanvasStartX,crabCanvasStartY);
		}
	
	private void updateXYIfCrabOnBorder(int crabX, int crabY, int crabCanvasX, int crabCanvasY){
		if(crabCanvasX == crabBoundary || crabCanvasX == canvasWidth - crabBoundary - CrabSizepx){
			mapCanvasDrawUpperLeftX = crabCanvasX - crabX;
		}
		if(crabCanvasY == crabBoundary || crabCanvasY == canvasHeight - crabBoundary- CrabSizepx ){
			mapCanvasDrawUpperLeftY = crabCanvasY - crabY;
		}

	}
	

	//Draws the Map
	public void draw(int crabX, int crabY, int crabCanvasX, int crabCanvasY){
		
		updateXYIfCrabOnBorder(crabX, crabY, crabCanvasX, crabCanvasY);

		drawBlackBackground(); //this fills in areas of the background if there is not enough map left
		canvas.getContext2d().drawImage(gameMapImage, mapCanvasDrawUpperLeftX, mapCanvasDrawUpperLeftY);
		
		clearCanvasBorders();
		paintBlackBorders();
		}
	
	public void setmapCanvasDrawUpperLeftXY(int drawX,int drawY){
		mapCanvasDrawUpperLeftX = drawX;
		mapCanvasDrawUpperLeftY = drawY;
	}
	
/*
	//Draws the Map where it told to
	public void forceMapDraw(int mapX, int mapY, int CanvasX, int CanvasY){

		updateXYIfCrabOnBorder(mapX, mapY, CanvasX, CanvasY);
		drawBlackBackground();//this fills in areas of the background if there is not enough map left
		canvas.getContext2d().drawImage(gameMapImage,
				mapDrawUpperLeftX, mapDrawUpperLeftY);

		clearCanvasBorders();
		paintBlackBorders();
		}
*/
	
	public void drawBlackBackground(){
		canvas.getContext2d().drawImage(blackBarImage,
				0,0,canvasWidth, canvasHeight);
	}
	
	private void clearCanvasBorders(){
		canvas.getContext2d().clearRect(0, 0,canvasWidth,50); //Clear top strip
		canvas.getContext2d().clearRect(0, 0,50,canvasHeight); //Clear Left strip
		canvas.getContext2d().clearRect(canvasWidth - 50, 0,50,canvasHeight); //Clear Right strip
		canvas.getContext2d().clearRect(0, canvasHeight - 50,canvasWidth,50); //Clear bottom strip
	}
	
	private void paintBlackBorders(){
		canvas.getContext2d().drawImage(blackBarImage, 0, 0,canvasWidth,50); //Black top strip
		canvas.getContext2d().drawImage(blackBarImage, 0, 0,50,canvasHeight); //Black Left strip
		canvas.getContext2d().drawImage(blackBarImage,canvasWidth - 50, 0,50,canvasHeight); //Black Right strip
		canvas.getContext2d().drawImage(blackBarImage,0, canvasHeight - 50,canvasWidth,50); //Black bottom strip
	}
	
	public int getCanvasUpperLeftDrawStartX(){
		return mapCanvasDrawUpperLeftX;
	}
	
	public int getCanvasUpperLeftDrawStartY(){
		return mapCanvasDrawUpperLeftY;
	}		
}
